<?php
namespace App\Http\Controllers\blog\front\contab;

use DB;
use App\Http\Controllers\blog\Controller;
use Symfony\Component\HttpFoundation\Request;
use Illuminate\Support\Facades\Redis;
use App\Model\front\UserModel;
use App\Http\Controllers\blog\front\contab\SendEmailController;
use Cache;
use Validator;

class AuthController extends Controller{


	public function login()
	{

		return view('blog/front/contab/login');

	}

	public function quick_login(Request $request)
	{
		if ($request->isMethod('post')) {
			$rule = [
					'email' => ['required', 'email'],
					'password' => 'required'
					];
			$msg = [
					'emial.required' => '请输入邮箱',
					'email.email' => 'Email不合法',
					'password' => '请输入密码'
					];
			$validator = Validator::make($request->input(),$rule,$msg);
			if ($validator->fails()) {
                //校验失败
                return response(['rs' => 0, 'msg' => current($validator->errors()->toArray())[0], 'data' => $validator->errors()->toArray()]);
            }
            //验证码验证
            if(empty($request->input('verify'))) return response(['rs' => 0, 'msg' => '验证码不能为空']);
            if (checkVerify($request->input('verify'),'web.login') === false) {
            	return response(['rs' => 0, 'msg' => '验证码错误']);
            }

            $user = new UserModel();
			$where = ['email'=>$request->input('email')];
			$info = $user->getInfoByEmail($where);
			if ($info) {
				if ($info->status != '0' && $info->status !='2') {
					$password = codePass($request->input('password'));
					if($password == $info->password){
						$data = ['username'=>$info->username,'user_id'=>$info->user_id];
						session(['userinfo'=>$data]);
						return response(['rs' => 1, 'msg' => '登陆成功~~页面即将跳转']);
					}else{
						return response(['rs' => 0, 'msg' => '密码错误']);
					}
				}elseif ($info->status == 0) {
					return response(['rs' => 0, 'msg' => '账户未激活']);
				}elseif ($info->status == 2) {
					return response(['rs' => 0, 'msg' => '账户已禁用']);
				}
			}else{
				return response(['rs' => 0, 'msg' => '账户不存在']);
			}



		}
	}

	public function register()
	{
		return view('blog/front/contab/register');
	}

	public function quick_register(Request $request)
	{
		if ($request->isMethod('post')) {
			$rule = [
					'email' => ['required', 'email', 'unique:user,email'],
					'username' => ['required', 'unique:user,username'],
					'password' => 'required'
					];
			$msg = [
					'username.required' => '请输入昵称',
					'username.unique' => '改昵称已被占用',
					'emial.required' => '请输入邮箱',
					'email.unique' => '该邮箱已被占用',
					'email.email' => 'Email不合法',
					'password' => '请输入密码'
					];
			$validator = Validator::make($request->input(),$rule,$msg);
			if ($validator->fails()) {
                //校验失败
                return response(['rs' => 0, 'msg' => current($validator->errors()->toArray())[0], 'data' => $validator->errors()->toArray()]);
            }
            //验证码验证
            if(empty($request->input('verify'))) return response(['rs' => 0, 'msg' => '验证码不能为空']);
            if (checkVerify($request->input('verify'),'web.login') === false) {
            	return response(['rs' => 0, 'msg' => '验证码错误']);
            }
            $data=[
            		'username'=>addslashes($request->input('username')),
            		'password'=> codePass($request->input('password')),
            		'email' =>addslashes($request->input('email')),
            		'register_ip' =>$request->getClientIp(),
            		'create_time' => time(),
            		'update_time' => time(),
            		'last_login_time' => time(),
            		'action_code' => emailCode(),
            		'last_login_ip' => $request->getClientIp(),
            	  ];
            $User = new UserModel();
            $res = $User->addUserInfo($data);
            if ($res) {
            	$info = ['email'=>$data['email'], 'name'=>$data['username'], 'uid'=>$res, 'activationcode'=>$data['action_code']];
            	$SendEmail = new SendEmailController();
            	$SendEmail->SendEmail($info);
            	return response(['rs' => 1, 'msg' => '注册成功！快去邮箱激活吧']);
            }
            return response(['rs' => 0, 'msg' => $res]);
		}
	}

	public function info()
	{
		//F2572722E5B3B6F10C7D7D5B42BFEA43
		//access_token=ACCBAFEF0174AB5AD38920376BDFF4D6&expires_in=7776000&refresh_token=EEECEFA0FDC465ADCE456F72097592CD
		//client_id":"1105380056","openid":"D1D266E7FE67241D331DD34A8227D75B
		$url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=1105380056&redirect_uri=http://www.weblei.net&state=332cee7b25503649e96e875648a6308b&scope=get_user_info";
		$url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=1105380056&client_secret=81TX2GzILkotXcmX&code=F2572722E5B3B6F10C7D7D5B42BFEA43&redirect_uri=http://www.weblei.net";
		$url = "https://graph.qq.com/oauth2.0/me?access_token=ACCBAFEF0174AB5AD38920376BDFF4D6";
		$url = "https://graph.qq.com/user/get_user_info?access_token=ACCBAFEF0174AB5AD38920376BDFF4D6&oauth_consumer_key=1105380056&openid=D1D266E7FE67241D331DD34A8227D75B";
		dd(urlencode('http://www.yiny520.com/weixin.php'));
		//https://open.weixin.qq.com/connect/qrconnect?appid=wx39a546680a2d5ef9&redirect_uri=http%3A%2F%2Fwww.weblei.net&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
	}
	//https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx39a546680a2d5ef9&redirect_uri=http%3A%2F%2Fwww.weblei.net%2Flogin&response_type=code&scope=snsapi_base&state=123#wechat_redirect 



}